﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

namespace Web.Core
{
    public class GridView
    {

        private static Int32 _idx = 1;

        public static void OnHtmlRowCreated(
            System.Web.UI.Page page,
            ASPxGridView grvResult,
            ASPxGridViewTableRowEventArgs e)
        {
            try
            {
                if (e.RowType == GridViewRowType.Data)
                {
                    ASPxLabel lbNo =
                        grvResult.FindRowCellTemplateControl(e.VisibleIndex, grvResult.Columns["No"] as GridViewDataColumn,
                                                             "lbNo") as ASPxLabel;

                    if (lbNo != null)
                    {
                        lbNo.Text = Running(
                                            page: page,
                                            index: grvResult.PageIndex,
                                            pageCount: grvResult.PageCount,
                                            idx: _idx);
                    }
                    _idx += 1;
                }
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex));
                //
            }
        }

        public static string Running(
            System.Web.UI.Page page,
            Int32 index,
            Int32 pageCount,
            Int32 idx)
        {
            try
            {
                return Convert.ToString(
                                        ((index*(pageCount + 1)) + idx)
                                        -
                                        ((index != 0) ? (pageCount + 1) : 0)
                                        );
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex));
                //
                return null;
            }
        }

        public static void HeaderChecking(
            System.Web.UI.Page page, 
            ASPxGridView grvResult)
        {
            try
            {
                ASPxCheckBox chkAll =
                    grvResult.FindHeaderTemplateControl(grvResult.Columns["Check"] as GridViewDataColumn, "chk") as
                        ASPxCheckBox;

                bool chkChecked = false;

                if (chkAll != null)
                    chkChecked = chkAll.Checked;

                for (Int32 i = 0; i <= grvResult.PageCount; i += 1)
                {
                    ASPxCheckBox chk =
                        grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Check"] as GridViewDataColumn,
                            "chk") as ASPxCheckBox;

                    if (chk != null)
                        chk.Checked = chkChecked;

                }
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex));
                //
            }
        }
    }
}